package ysq.xyg.base.util;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 单例获取百度图片文字识别
 *
 * @author taocongcong
 * @create 2019-05-05 9:23
 */
public class AipSingleton {

    private final static String APP_ID = "16166751";

    private final static String APP_KEY = "AGhE3GvRGFMdbQ32vIgLNu8a";

    private final static String SECRET_KEY = "RGUZjNzo3dUR1Lh7ZqEauHUwPuRnGhqs";

    private static AipOcr aipOcr;

    public static AipOcr getAipOcrInstance() {
        try {
            if (null == aipOcr) {
                synchronized (AipOcr.class) {
                    if(null == aipOcr) {
                        aipOcr = new AipOcr(APP_ID, APP_KEY, SECRET_KEY);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aipOcr;
    }
    public static JSONObject acceptText(AipOcr aipOcr,String url) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");

        // 参数为本地路径
       /* String image = "C:\\logs\\mmexport1557017929937.jpg";
        JSONObject res = client.basicGeneral(image, options);*/
        // 通用文字识别, 图片参数为远程url图片
        JSONObject res = aipOcr.basicGeneralUrl(url, options);
       return res;
    }
}
